package main

import (
	"encoding/json"
	"igxe-change/model"
	"igxe-change/worker"
	"io/ioutil"
)

func main() {

	jsonConfig := &model.Config{}

	data, err := ioutil.ReadFile("./config.json")

	if err != nil {
		panic(err)
	}

	err = json.Unmarshal(data, jsonConfig)

	if err != nil {
		panic(err)
	}



	users := jsonConfig.Users

	changeWorkChan := make(chan worker.Job)

	for _, user := range users {
		go func(user model.User) {
			job := worker.SimpleJob{
				User: &user,
			}

			changeWorkChan <- job
		}(user)
	}

	for {
		job := <-changeWorkChan

		go func(j worker.Job) {
			jr := j.Execute()

			newJob := worker.SimpleJob{
				User: &jr.User,
			}

			changeWorkChan <- newJob
		}(job)
	}
}
